<!-- 任务分配 -->
<!-- 接取任务 -->
<template>
  <div>
    <div class="searchbox">
      <el-form :inline="true" size="small" label-width="100px">
        <jt-easy-collapse :has-expand="true">
          <template slot="expand">
                    <el-form-item label="任务单号">
                      <el-input size="small" v-model="form.taskNo" placeholder="任务单号"></el-input>
                    </el-form-item>
                    <el-form-item label="零件名称">
                      <el-input size="small" v-model="form.partName" placeholder="零件名称"></el-input>
                    </el-form-item>
                    <el-form-item label="零件号">
                      <el-input size="small" v-model="form.partNumber" placeholder="零件号"></el-input>
                    </el-form-item>
                    <el-form-item label="热处理炉号">
                      <el-input size="small" v-model='form.heatingNumber' placeholder="热处理炉号"></el-input>
                    </el-form-item>
                    <el-form-item label="零件图号">
                      <el-input size="small" v-model="form.partGraphNumber" placeholder="零件图号"></el-input>
                    </el-form-item>
                    <el-form-item label="样品阶段">
                      <!-- <el-input size="small" v-model="form.sampleStatusPhase" placeholder="样品阶段"></el-input> -->
                      <el-select v-model="form.sampleStatusPhase" placeholder="请选择" >
                         <el-option  :label="all" :value="' '"></el-option>
                        <el-option v-for="item in sampleStatusPhaseList" :key="item.code" :label="item.name" :value="item.code">
                        </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="项目号">
                      <el-input size="small" v-model="form.projectNo" placeholder="项目号"></el-input>
                    </el-form-item>
                    <el-form-item label="报验批次信息">
                      <el-input size="small" v-model="form.batchNumber" placeholder="报验批次信息"></el-input>
                    </el-form-item>
                    <el-form-item label="材料牌号">
                      <el-input size="small" v-model="form.materialNo" placeholder="材料牌号"></el-input>
                    </el-form-item>
                    <el-form-item style="margin-left: 20px;">
                      <el-button size="mini" type="primary" @click="showList">查询</el-button>
                    </el-form-item>
                    <el-form-item>
                      <el-button size="mini"@click='clearForm'>清除</el-button>
                    </el-form-item>
</template>
        </jt-easy-collapse>
      </el-form>
    </div>
    <div class="contentbox">
      <div style="float: left;padding-left: 20px;font-size: 15px;color: #666;padding-top: 6px;">
        接取任务信息 共 <span style="color: red;">{{pageTotal}}</span> 条数据
      </div>
      <el-row style="float: right;margin-right: 20px;margin-bottom: 15px;" type="flex" justify="end">
        <el-col>
          <jt-table-column-filter :table-key.sync="tableKey" :header-data.sync="tableHeaderArr" tableName='taskAllocation' :filter-column.sync="filterColumnData"></jt-table-column-filter>
          <!-- 分配任务 -->
          <!-- <assign-task-reminder-button :show.sync="isShowAssign" style="display: inline-block;"></assign-task-reminder-button> -->
          <!-- <el-button size="small" type="primary">接收任务</el-button> -->
          <!-- <el-button size="small" type="danger">退回</el-button> -->
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" :key='tableKey' v-loading="loading" @selection-change="handleSelectionChange" border stripe>
        <el-table-column :index="indexMethod" type="index" label="序号" width="50" align="center" :resizable="false"></el-table-column>
        <el-table-column align="center" :key='item' v-for='item in filterColumnData' :resizable="false"
            :label="getTableHeaderLabel(item)"
            :prop="item"
            :min-width="getTableColumnWidth(item)"
            :fixed="getTableColumnFixed(item)"
            :sortable="getTableColumnSort(item)">
<template slot-scope="scope">
   {{scope.row[item]}}
  <el-button v-if="item === 'operate'" size="small" type="text" @click="acceptTask(scope.row)">接受任务</el-button>
</template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizeArr"
          :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal"></el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
  import {
    getTobeAssignedProcessListService,
    postTakeTaskService,
    getSampleStatusPhase
  } from 'service/task';
  import {
    APPLY_TYPE_LIST
  } from 'service/constant';
  import {
    getLableFromList,
    dateFormat
  } from 'utils/common';
  import JtTableColumnFilter from 'components/jt-table-column-filter/index'; // 导入表格列筛选组件
  import JtEasyCollapse from 'components/jt-easy-collapse/index';
  import AssignTaskReminderButton from 'dialog/experimental-management/task-allocation/assign-task-reminder-button';
  import {
    getSampleStatusStageList
  } from '../../../../service/task';
  export default {
    name: 'TaskAllocation',
    components: {
      'jt-table-column-filter': JtTableColumnFilter,
      'jt-easy-collapse': JtEasyCollapse,
      'assign-task-reminder-button': AssignTaskReminderButton,
    },
    data() {
      return {
        tableKey: 1, // table key
        filterColumnData: [],
        sampleStatusPhaseList: [],
        all:'全部',
        tableHeaderArr: [{
            'name': 'acceptTime',
            'alias': '样品接收时间',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'applyTime',
            'alias': '创建时间',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'applyTypeStr',
            'alias': '实验类型',
            'default': false,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'taskNo',
            'alias': '任务单号',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'heatingNumber',
            'alias': '热处理炉号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
               {
            'name': 'partName',
            'alias': '零件名称',
            'default': true,
            'width': 170,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'partNumber',
            'alias': '零件号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'applyUserName',
            'alias': '申请人',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          // {
          //   'name': 'operateUserName',
          //   'alias': '操作人',
          //   'default': true,
          //   'width': 230,
          //   'fixed': false,
          //   'sort': true
          // },
          {
            'name': 'projectNo',
            'alias': '项目号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'sampleStatusPhaseStr',
            'alias': '样品阶段',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'materialNo',
            'alias': '材料牌号',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'batchNumber',
            'alias': '报验批次信息',
            'default': true,
            'width': 230,
            'fixed': false,
            'sort': true
          },
          {
            'name': 'operate',
            'alias': '操作',
            'default': true,
            'width': 190,
            'fixed': 'right',
            'sort': false
          }
        ],
        currentPage: 1,
        pageSizeArr: [10, 20, 50, 100],
        pageSize: 10,
        pageTotal: 10,
        form: {
          taskNo: '',
          partName: '',
          partNumber: '',
          partGraphNumber: '',
          sampleStatusPhase: '',
          projectNo: '',
          batchNumber: '',
          materialNo: ''
        },
        loading: false,
        tableData: [],
        isShowAssign: false,
      }
    },
    mounted() {
      getSampleStatusPhase().then(res => {
        this.sampleStatusPhaseList = res.data.result;
      })
      this.showList();
    },
    methods: {
       // 清除
      clearForm() {
        this.form = {};
        this.showList();
      },
       indexMethod(index) {
        return index + this.pageSize * (this.currentPage-1) + 1;
      },
      change(index) {
        this.changeRed = index;
      },
      // 任务领取
      acceptTask(row) {
        this.$confirm('是否接取此任务单?', '提示', {
          type: 'warning'
        }).then(() => {
          postTakeTaskService(row.id)
            .then(res => {
              if (res.data.success) {
                 this.$message({
            message: `已接取的任务单号为：${row.taskNo}`,
            type: 'success',
            duration: 1000
            });
                this.showList();
              } else {
                this.$alert(res.data.message, '提示', {
                  type: 'error'
                })
              }
            })
        }).catch(() => {});
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.showList();
      },
      handleCurrentChange(val) {
        this.pageNumber = val;
        this.currentPage = val;
        this.showList();
      },
      // 表格数据展示
      showList() {
        this.loading = true;
        getTobeAssignedProcessListService(this.form.taskNo,
            this.form.partNumber,
            this.form.batchNumber,
            this.form.sampleStatusPhase,
            this.form.projectNo,
            this.form.partName,
            this.form.partGraphNumber,
            this.form.materialNo,
            this.form.heatingNumber,
            this.currentPage,
            this.pageSize,
            '', '')
          .then(res => {
            this.tableData = res.data.result.content;
            // this.currentPage = res.data.result.pageable.pageNumber;
            this.pageSize = res.data.result.pageable.pageSize;
            this.pageTotal = res.data.result.totalElements;
            this.loading = false;
            if (this.tableData) {
              for (let i = 0; i < this.tableData.length; i++) {
                this.tableData[i].taskTypeStr = getLableFromList(APPLY_TYPE_LIST, this.tableData[i].taskType);
                this.tableData[i].applyUserName = this.tableData[i].applyUser ? this.tableData[i].applyUser.name : '';
                this.tableData[i].applyTimeStr = dateFormat(new Date(this.tableData[i].applyTime));
                this.tableData[i].sampleStatusPhaseStr = getLableFromList(this.sampleStatusPhaseList, this.tableData[i].sampleStatusPhase);
              }
            }
          });
      },
      // 分配任务
      // allotTask() {
      //   this.isShowAssign = true;
      // }
      // 获取表格多选的值
      handleSelectionChange(val) {},
      getTableHeaderLabel(str) {
        return this.getTableSomeValue(str, 'alias');
      },
      // 获取表格列的宽度
      getTableColumnWidth(str) {
        return this.getTableSomeValue(str, 'width');
      },
      // 获取表格列是否是固定的
      getTableColumnFixed(str) {
        return this.getTableSomeValue(str, 'fixed');
      },
      // 获取表格列是否排序
      getTableColumnSort(str) {
        return this.getTableSomeValue(str, 'sort');
      },
      // 获取表格表头，列宽度，列固定，列排序的值
      getTableSomeValue(str, keyName) {
        for (var value of this.tableHeaderArr) {
          if (value['name'] == str) {
            return value[keyName];
          }
        }
      }
    },
  }
</script>
